package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.core.constant.GlobalConstants;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.asiainfo.common.redis.utils.RedisUtils;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysLoginInfoService;
import org.asiainfo.system.domain.vo.SysLoginInfoVo;
import org.asiainfo.system.domain.bo.SysLoginInfoBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;

import java.util.List;

/**
 * 系统登录记录
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/loginInfo")
public class SysLoginInfoController extends BaseController {

    private final SysLoginInfoService sysLoginInfoService;

    /**
     * 分页查询
     *
     * @param sysLoginInfoBo 筛选条件
     * @param pageQuery      分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:loginInfo:list")
    @GetMapping("/list")
    public TableDataInfo<SysLoginInfoVo> list(SysLoginInfoBo sysLoginInfoBo, PageQuery pageQuery) {
        return sysLoginInfoService.queryPageList(sysLoginInfoBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param infoId 主键
     * @return 单条数据
     */
    @SaCheckPermission("system:loginInfo:query")
    @GetMapping("/{infoId}")
    public R<SysLoginInfoVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("infoId") Long infoId) {
        return R.ok(sysLoginInfoService.queryById(infoId));
    }

    /**
     * 删除数据
     *
     * @param infoIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:loginInfo:remove")
    @Log(title = "系统登录记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long[] infoIds) {
        return toAjax(sysLoginInfoService.deleteWithValidByIds(List.of(infoIds)));
    }

    /**
     * 清空系统登录日志
     *
     * @return 操作结果
     */
    @SaCheckPermission("system:loginInfo:remove")
    @Log(title = "系统登录记录", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R<Void> clean() {
        sysLoginInfoService.cleanLoginInfo();
        return R.ok();
    }

    /**
     * 解锁用户
     *
     * @param userName 用户名
     * @return 操作结果
     */
    @SaCheckPermission("system:loginInfo:unlock")
    @Log(title = "系统登录记录", businessType = BusinessType.OTHER)
    @GetMapping("/unLock/{userName}")
    public R<Void> unLock(@NotEmpty(message = "用户名不能为空") @PathVariable String userName) {
        String loginName = GlobalConstants.PWD_ERR_CNT_KEY + userName;
        if (RedisUtils.hasKey(loginName)) RedisUtils.deleteObject(loginName);
        return R.ok();
    }
}

